*************************************************
*************************************************
***Estimating diffusion parameters
*************************************************
*************************************************


************************************
****Figure 3 from Section 5.2
*************************************

**Use file from Santacreu (2023, RESTAT)


use Reduced_form.dta , clear
bys iso3_d: egen GPindex=mean(overall_d)
keep iso3_d GPindex
duplicates drop
sort iso3_d 
save tempo, replace


use Reduced_form.dta , clear
sort iso3_d
merge iso3_d using tempo
assert _m==3
drop _m

sort iso3_o iso3_d year
save tempo, replace



**Drop tax havens

drop if iso3_o=="IRL" || iso3_o=="LUX" || iso3_o=="NLD" || iso3_o=="CHE" || iso3_o=="SGP" || iso3_o=="HKG"|| iso3_o=="BEL"|| iso3_o=="PAN" || iso3_o=="MLT"|| iso3_o=="HUN"
drop if iso3_d=="IRL" || iso3_d=="LUX" || iso3_d=="NLD" || iso3_d=="CHE" || iso3_d=="SGP" || iso3_d=="HKG"|| iso3_d=="BEL"|| iso3_d=="PAN" || iso3_d=="MLT"|| iso3_d=="HUN"

drop exp_time imp_time asym_pair c1 c2

* create exporter-time and importer-time IDs
egen exp_time = group(iso3_o year)
egen imp_time = group(iso3_d year)
egen imp_constant = group(iso3_d)
egen exp_constant = group(iso3_o)



* create symmetric pair ID
gen c1 = iso3_o
gen c2 = iso3_d
**replace c1 = iso3_d if iso3_o<iso3_d
**replace c2 = iso3_o if iso3_o<iso3_d
egen asym_pair = group(c1 c2)
**keep if year<=2000
gen lGPindex = log(GPindex)
replace rta_tech = 0 if iso3_o==iso3_d
replace rta_notech = 0 if iso3_o == iso3_d
eststo clear
eststo: ppmlhdfe royalt rta_tech rta_notech , nocons absorb(exp_time imp_time  asym_pair, savefe) cluster(asym_pair)
save Regression_analysis, replace


**Obtaining the probability of imitation

use  Regression_analysis, clear
keep if year<2000
replace lGP = log(overall_d)
collapse __hdfe2 lGP gdpcap_d gdp_d, by(iso3_d year)
gen lgdpcap_d=log(gdpcap_d)
gen lgdp_d = log(gdp_d)
**reg __hdfe2 lGP  lgdp_d lgdpcap_d  if year<2000
**gen lprob =_[_cons]+ _b[lGP]*lGP

xi: reg __hdfe2 lGP lgdp_  i.iso  if year<2000
predict lprob
gen prob=exp(_b[lGP]*lGP)

**Another option:
*xi: reg __hdfe2  lgdp_  i.iso  if year<2000
**get eta from fixed effect relative to US and corr with GP


collapse prob lgdpcap, by( year iso3_d)
sort year iso3_d
replace iso3_d = "ZZZ" if iso3_d=="USA"
drop if prob==.
sort year iso3_d 
bys year: gen probrelUS = (prob)/(prob[_N])
line probrelUS year if iso=="CHN" || line probrelUS year if iso=="ZZZ" || line probrelUS year if iso=="BRA"
**keep if year<=2000
collapse probrel lgdpcap, by(iso)
sort probrel
edit
sort iso
gen gdpcaprel=exp(lgdpcap-lgdpcap[_N])
scatter probrel lgdpcap

**replace probrel = 1-probrel
replace iso="USA" if iso=="ZZZ"


**********************************
*******Figure 3*******************
**********************************

scatter probrel lgdpcap, mlabel(iso) ytitle(Quality of IP enforcement (rel. US)) ytitle(, size(large)) ylabel(, labsize(medium)) xtitle(GDP pc (rel. US)) xtitle(, size(large)) xlabel(, labsize(medium)) legend(off) graphregion(fcolor(white)) || lfit probrel lgdpcap
graph save Graph "ProbrelGDPpc.gph", replace
graph export  "ProbrelGDPpc.eps", as(eps)replace
graph export "ProbrelGDPpc.pdf", as(pdf) replace


********************************************************************************************************
********************************************************************************************************
********************************************************************************************************
********************************************************************************************************
**Structural gravity for royalty payments to estimate diffusion parameters in calibration Section 5.1
********************************************************************************************************
********************************************************************************************************
********************************************************************************************************
********************************************************************************************************


**Estimating eps
use Regression_analysis, clear
keep if year<=2000
gen roy_gdp=royalt/gdp_d
eststo clear
**eststo: ppmlhdfe roy_gdp  rta_tech rta_notech ,  absorb(exp_time imp_time  asym_pair, savefe) cluster(asym_pair)
eststo: ppmlhdfe royalt  rta_tech rta_notech ,  absorb(exp_time imp_time  asym_pair, savefe) cluster(asym_pair)

keep if year==2000

collapse __hdfe3__ dist royalt comlang_off contig gdp_d gdpcap_d, by(iso3_o iso3_d)
duplicates drop
***For a given growth rate g=4*1.87



gen pair = __hdfe3
gen ldist=log(dist)
gen exppair = exp(__hdfe3)
egen imp_constant = group(iso3_d)
egen exp_constant = group(iso3_o)



ppmlhdfe exppair ldist comlang_off contig, absorb(imp_constant exp_constant, savefe) d(res)
predict pairfe

replace pair = pairfe if pair==.


sort iso3_o iso3_d
gen id = pair if iso3_o==iso3_d
bys iso3_d: egen  domestic_trade_cost = sum(id)

replace pairfe = pair
gen g=0.0185*4
gen pairferel = exp(pairfe-domestic_trade_cost)
drop if iso3_o==iso3_d

egen maxpair = max(pairferel)
egen minpair = max(-pairferel)
replace minpair = -minpair
gen normpair = 0.99*(pairferel-minpair)/(maxpair-minpair)+0.01

gen pairtest = normpair

collapse pairtest gdp_ ldist royalt, by(iso3_o iso3_d)
gen g=0.0185*4
**Using the 25% patenting rule
gen rho = 0.25

gen ratio = pairtest*0.5/(0.5+g)
gen eps = ratio*g/(1-ratio)



gen dummy_o=1 if iso3_o=="USA"
replace dummy_o=3 if iso3_o=="CHN"
replace dummy_o=2 if dummy_o==.

gen dummy_d=1 if iso3_d=="USA"
replace dummy_d=3 if iso3_d=="CHN"
replace dummy_d=2 if dummy_d==.


gen roy_gdp = roy/gdp
replace eps = eps*100
collapse (mean) eps  [iweigh=gdp_d ], by(dummy_o dummy_d)
**collapse (mean) eps, by(dummy_o dummy_d)

replace eps = eps
**drop if dummy_o==dummy_d
reshape  wide eps, i(dummy_d) j(dummy_o) 
 
 
 
 
 
edit









